//
//  TestQuestionsViewController.m
//  WheatPlan
//
//  Created by 王晨辉 on 15/5/29.
//  Copyright (c) 2015年 IOSTeam. All rights reserved.
//

#import "TestQuestionsViewController.h"
#import "TestQuestionsReportViewController.h"
#import "TestQuestionTableViewCell.h"

//#import "NewFileTableViewCell.h"
@interface TestQuestionsViewController (){
    
    int a;
    int i ;
    BOOL ifRead;
    UIButton * RegistBtn;
    NSMutableDictionary *mutableDictionary_allQuesitons;// all questions
    NSMutableDictionary *mutableDictionary_alltask;// all questions

    
}
@end

@implementation TestQuestionsViewController


-(void)viewWillAppear:(BOOL)animated{
    
    

    
}
- (void)viewDidLoad {
 
    
    [super viewDidLoad];
    //设置导航栏头文字
    //设置导航栏背景颜色
    [self.navigationController.navigationBar setBarTintColor:[GetFileModel colorWithHexString:@"#44afd7" alpha:1]];
    //设置导航栏字体颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
    self.view.backgroundColor = [UIColor whiteColor];
    
    i = 0;
    
    
    
    
    //右边按钮
    UIBarButtonItem *right = [[UIBarButtonItem alloc]
                              initWithTitle:@"退出"
                              style:UIBarButtonItemStylePlain
                              target:self
                              action:@selector(turntolast)];
    [right setTintColor:[UIColor whiteColor]];
    
    self.navigationItem.rightBarButtonItem = right;
    
    
    
    //右边按钮
    lef = [[UIBarButtonItem alloc]
                              initWithTitle:@"上一题"
                              style:UIBarButtonItemStylePlain
                              target:self
                            action:@selector(rightBtnClick:)];
    [lef setTintColor:[UIColor whiteColor]];
    lef.tag = 200;
    

    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
//    _tableView.rowHeight = 150;
    _tableView.separatorStyle = 0;
    
    

    
    _muArrau = [NSMutableArray array];
    _muArray = [NSMutableArray array];
    
    mutableDictionary_allQuesitons = [NSMutableDictionary dictionary];

    [self.view addSubview:_tableView];
    
    
    [self subView];

    
    _number = [NSMutableArray array];
    dic = [NSMutableDictionary dictionary];
    nameDic = [NSMutableDictionary dictionary];

    
    
}
-(void)bout:(UIButton *)butt atCell:(UITableViewCell *)cell{
    
    
    
    
}

-(void)turntolast{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
}
-(void)subView{
    
    
    

    
    _tLab = [[UILabel alloc]initWithFrame:CGRectMake(0, SCREEN_WIDTH/3.3, SCREEN_WIDTH, 150)];
    _tLab.numberOfLines = 0;
    _tLab.font = [UIFont systemFontOfSize:15];
    _tLab.textAlignment = NSTextAlignmentCenter;
    // _tLab.backgroundColor = [UIColor redColor];
    [self.view addSubview:_tLab];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    
    
    
    ////NSLog(@"%@",self.situationID);

    
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *userId = [user objectForKey:@"ID"];
    //NSLog(@"我看看是不是%@",self.typeID);
    
    if (![self.typeID isEqual:@"1"]) {
        
        
        param2 = @{@"userId": userId,@"babyId": [BabyManager shareInstance].babyId,@"typeId": self.typeID};
        
    }else{
        
        
        param2 = @{@"userId": userId,@"babyId": [BabyManager shareInstance].babyId,@"typeId": @"1",@"situationId":self.situationID};

    }

    
    [UserDAL startEvaluationParams:param2 withBlock:^(NSDictionary *data, NSError *error) {
        
        if (data) {
            
            ////NSLog(@"%@",data);
            
//            ////NSLog(@"get data as :%@",data[@"dataList"]);
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            
            
                NSArray *myArray = data[@"dataList"];
                NSString * task = data[@"taskId"];
                NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
                [user setObject:task forKey:@"task"];
                ////NSLog(@"%@是他是他就是他",user);
                
            if (myArray.count!=0){

                for( NSDictionary *tmp in myArray){
                    
                    [mutableDictionary_allQuesitons setObject:tmp[@"question"] forKey:tmp[@"id"]];
                    
                }
                [_tableView reloadData];
                
                
            }else{
                
                alert(@"暂无适合您宝宝的测题");
                
                
            }

            
            
        }else{
            
            
            
            alert(@"暂无适合您宝宝的测题");

            
        }
            
    }];

    
    

    


    
    
    
    
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{
    NSString *msg = [[NSString alloc] initWithFormat:@"您点击的是第%d个按钮!",buttonIndex];
    ////NSLog(@"msg:%@",msg);
    if (buttonIndex == 0) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
        ////NSLog(@"1111111");
        
    }else if(buttonIndex == 1){
        
        
        ////NSLog(@"2222222");
        
        
    }
    
    
}

-(void)rightBtnClick:(UIButton *)button{
    _tLab.text = @"";
    if (button.tag == 100) {
        i+=1;

        
        if (i<1) {
            
            self.navigationItem.leftBarButtonItem = nil;
            
            
        }else{
            
            self.navigationItem.leftBarButtonItem = lef;
            
        }

        if (i>mutableDictionary_allQuesitons.allValues.count-1) {
            ////NSLog(@"1111");
            i =  mutableDictionary_allQuesitons.allValues.count-1;
            [dic setObject:@"0" forKey:[NSString stringWithFormat:@"%@",mutableDictionary_allQuesitons.allKeys[i]]];

            TestQuestionsReportViewController * testQVC = [[TestQuestionsReportViewController alloc]init];
            testQVC.dic = nameDic;
            testQVC.dicID = dic;
            testQVC.typeID = self.typeID;
            testQVC.number = @"1";
            testQVC.allval = mutableDictionary_allQuesitons.allValues;
//            testQVC.allkey = mutableDictionary_allQuesitons.allKeys;
            testQVC.taskId = [NSString stringWithFormat:@"%@",mutableDictionary_alltask.allValues];
            UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:testQVC];
            [self presentViewController:nav animated:YES completion:nil];

            
            
            
        }
        
        
        
        _tLab.text = [NSString stringWithFormat:@"      %@",mutableDictionary_allQuesitons.allValues[i]];
        self.navigationItem.title =[NSString stringWithFormat:@"   第%d题/共%lu题",i+1,(unsigned long)mutableDictionary_allQuesitons.allValues.count];
        [dic setObject:@"0" forKey:[NSString stringWithFormat:@"%@",mutableDictionary_allQuesitons.allKeys[i-1]]];

    }else if (button.tag == 300){
        
        
        i+=1;
        
        
        if (i<1) {
            
            self.navigationItem.leftBarButtonItem = nil;
            
            
        }else{
            
            self.navigationItem.leftBarButtonItem = lef;
            
        }
        
        if (i>mutableDictionary_allQuesitons.allValues.count-1) {
            i =  mutableDictionary_allQuesitons.allValues.count-1;
            [dic setObject:@"1" forKey:[NSString stringWithFormat:@"%@",mutableDictionary_allQuesitons.allKeys[i]]];
            TestQuestionsReportViewController * testQVC = [[TestQuestionsReportViewController alloc]init];
            testQVC.dic = nameDic;
            testQVC.dicID = dic;
            testQVC.typeID = self.typeID;
            testQVC.number = @"1";
            testQVC.allval = mutableDictionary_allQuesitons.allValues;
//            testQVC.allkey = mutableDictionary_allQuesitons.allKeys;
            testQVC.taskId = [NSString stringWithFormat:@"%@",mutableDictionary_alltask.allValues];
            UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:testQVC];
            [self presentViewController:nav animated:YES completion:nil];

            
        }
        
        _tLab.text = [NSString stringWithFormat:@"      %@",mutableDictionary_allQuesitons.allValues[i]];
        self.navigationItem.title =[NSString stringWithFormat:@"   第%d题/共%lu题",i+1,(unsigned long)mutableDictionary_allQuesitons.allValues.count];
        [dic setObject:@"1" forKey:[NSString stringWithFormat:@"%@",mutableDictionary_allQuesitons.allKeys[i-1]]];
//        ////NSLog(@"%@,aaaaaaaaaa",dic);

        
    }else if (button.tag == 200){
        i-=1;
//        ////NSLog(@"%d,aaaaaaaaaa",i);
        
        if (i<1) {
            i = 0;
            self.navigationItem.leftBarButtonItem = nil;
            
            
        }else{
            
            self.navigationItem.leftBarButtonItem = lef;
            
        }
        
        _tLab.text = [NSString stringWithFormat:@"      %@",mutableDictionary_allQuesitons.allValues[i]];
        self.navigationItem.title =[NSString stringWithFormat:@"   第%d题/共%lu题",i+1,(unsigned long)mutableDictionary_allQuesitons.allValues.count];

        
        
    }
    
    
    
    
}
-(void)turntoChoose{
    
    
    
    TestQuestionsReportViewController * testQVC = [[TestQuestionsReportViewController alloc]init];
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:testQVC];
    [self presentViewController:nav animated:YES completion:nil];
    
    
}
#pragma mark--- tableViewDelegate 每个区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
    
}

#pragma mark--- tableViewDelegate 区尾的高度

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0;
    
}
#pragma mark--- tableViewDelegate cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%ld%ld", (long)[indexPath section], (long)[indexPath row]];//以indexPath来唯一确定cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        
        

//        cell.backgroundColor = [UIColor redColor];
        
    }
    
//    CGRectGetMaxY(<#CGRect rect#>)
    

    if (indexPath.row == 0) {
        
        _tableView.rowHeight = SCREEN_HEIGHT;
        if (mutableDictionary_allQuesitons.allValues.count!=0) {
            
            ////NSLog(@"题目:%@",mutableDictionary_allQuesitons.allValues[0]);
            self.navigationItem.title =[NSString stringWithFormat:@"   第1题/共%lu题",(unsigned long)mutableDictionary_allQuesitons.allValues.count];

            _tLab = [[UILabel alloc]initWithFrame:CGRectMake(12, 0, SCREEN_WIDTH-24, 200)];
            _tLab.numberOfLines = 0;
            _tLab.font = [UIFont systemFontOfSize:17];
            _tLab.textAlignment = NSTextAlignmentLeft;
            [cell addSubview:_tLab];
            
            
            UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [rightBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            rightBtn.frame = CGRectMake(30,_tableView.rowHeight-100-100-30, SCREEN_WIDTH-60, 50);
            rightBtn.tag = 100;
            [cell addSubview:rightBtn];
            
            
            UIButton * noBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [noBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            noBtn.frame = CGRectMake(30, _tableView.rowHeight-100-50, SCREEN_WIDTH-60, 50);

            noBtn.tag = 300;
            [cell addSubview:noBtn];

            
            if ([self.typeID isEqualToString:@"0"]) {
                //NSLog(@"有类似");
                
                [rightBtn setBackgroundImage:[UIImage imageNamed:@"答题-有现象@2x.png"] forState:UIControlStateNormal];
                [noBtn setBackgroundImage:[UIImage imageNamed:@"答题-无现象@2x.png"] forState:UIControlStateNormal];
                
            } else if ([self.typeID isEqualToString:@"1"]) {
                //NSLog(@"是");
                [rightBtn setBackgroundImage:[UIImage imageNamed:@"_0002_是.png"] forState:UIControlStateNormal];
                [noBtn setBackgroundImage:[UIImage imageNamed:@"_0003_不是.png"] forState:UIControlStateNormal];

            }else{
                
                //NSLog(@"能");
                [rightBtn setBackgroundImage:[UIImage imageNamed:@"_0000_能.png"] forState:UIControlStateNormal];
                [noBtn setBackgroundImage:[UIImage imageNamed:@"_0001_不能.png"] forState:UIControlStateNormal];

                
            }

            

            
            

            if (i!=0) {
                
                
                
            }else{
                
                
                _tLab.text = [NSString stringWithFormat:@"      %@",mutableDictionary_allQuesitons.allValues[i]];

                
            }
            
            
        }


        
        

    }else{
        
        _tableView.rowHeight = 60;
        
        if (indexPath.row == 1) {
            
            
            UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [rightBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            rightBtn.frame = CGRectMake(30,0, SCREEN_WIDTH-60, 50);
            [rightBtn setBackgroundImage:[UIImage imageNamed:@"答题-能够@2x.png.png"] forState:UIControlStateNormal];
            rightBtn.tag = 100;
            [cell addSubview:rightBtn];

            
            
        }else{
            
            

            UIButton * noBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [noBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            noBtn.frame = CGRectMake(30, 10, SCREEN_WIDTH-60, 50);
            [noBtn setBackgroundImage:[UIImage imageNamed:@"答题-不能够@2x.png.png"] forState:UIControlStateNormal];
            noBtn.tag = 300;
            [cell addSubview:noBtn];

            
        }

    }
    
    
    
    
    return cell;
    
   
    
}


- (void) didClickButtonAtIndex:(SelectionButton)button atCell:(CheckBoxTableViewCell *)cell andIndex:(NSInteger)index {
    
    i+=1;
    if (i == _muArrau.count) {
        i = -1;
    }else{
        
        NSString * str =  [NSString stringWithFormat:@"   第%d题：%@",i+1,_muArray[i]];
        cell.titiLab.text = str;
        ////NSLog(@"%lu,%lu",(unsigned long)str.length,(unsigned long)_muArray.count);

        
    }
    
    switch (button) {
        case SelectionButtonEnabled:
            
        {
            numObj = [NSNumber numberWithInt:1];

            
        }
            break;
            
        default:
            
        {
            numObj = [NSNumber numberWithInt:0];

            break;
        }
    }
    
    
    

    
    
    
   
    [dic setObject:numObj forKey:[NSString stringWithFormat:@"%@",_muArrau[i]]];

    
    NSDictionary *dict =[[NSDictionary alloc] initWithObjectsAndKeys:dic,@"Myddic", nil];
    
    
    ////NSLog(@"%@",dict);

    
    
        
        RegistBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-100, 5,80, 30)];
        
        [RegistBtn setTitle:@"完成" forState:UIControlStateNormal];
        
        [RegistBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [RegistBtn addTarget:self action:@selector(turntoChoose) forControlEvents:UIControlEventTouchUpInside];
        
        [self.navigationController.navigationBar addSubview:RegistBtn];

        
    
    
}





-(void)addTag:(UIButton *)mySwitch1{
    
    mySwitch1.selected = !mySwitch1.selected;
    mySwitch1.tag = -mySwitch1.tag;
    ifRead = mySwitch1.selected;

    
    numObj = [NSNumber numberWithInt:1];
    [dic setObject:numObj forKey:[NSString stringWithFormat:@"%@",_muArrau[(long)index]]];
    ////NSLog(@"%@",dic);
    
    
    
}

-(NSMutableDictionary *) ppd:(NSMutableDictionary *)dic1{

    return dic1;
    
}


-(void)addTag1:(UIButton *)mySwitch {
    
    mySwitch.selected = !mySwitch.selected;
    mySwitch.tag = -mySwitch.tag;
    ifRead = mySwitch.selected;
    
    if (mySwitch.selected == 0) {
        

        

        [RegistBtn removeFromSuperview];
        
        

    } else {
        
        
  
        ////NSLog(@"1");
        
    }

//    ////NSLog(@"%ld",(long)mySwitch.tag);
    
    
}
-(void)addTag2:(UIButton *)mySwitch2{
    
    mySwitch2.selected = !mySwitch2.selected;
    mySwitch2.tag = -mySwitch2.tag;
    ifRead = mySwitch2.selected;
    
    ////NSLog(@"%ld",(long)mySwitch2.tag);
    
}
#pragma mark--- tableViewDelegate 有几个区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
    
}
#pragma mark--- tableViewDelegat 点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
 

    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
